package cn.co.Servlet;

import cn.co.entry.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/LoginServelt")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=UTF-8");
        //用HttpServletRequest对象的getParameter()方法获取用户名和密码
        PrintWriter out = resp.getWriter();
        HttpSession session = req.getSession();
        String username=req.getParameter("username");
        String password=req.getParameter("password");

        //假设用户名和密码分别为itcast和123
        if (("itcast").equals(username)&&("123").equals(password)){
            User user=new User(username,password);
            //如果用户名和密码正确，重定向到welcome.html
            session.setAttribute("user", user);
            resp.sendRedirect("/javawebshiyan2_war_exploded/indexServlet");
        } else {
            //如果用户名和密码错误，重定向到login.html
            out.print("用户名或密码错误");
            resp.setHeader("refresh","2;url='/javawebshiyan2_war_exploded/login.html'");
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}